import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
var _excluded = ["description", "scrollWidth", "wrapStyle", "singleCol", "sortInfo"];

var _dec, _class;

import React, { PureComponent } from 'react';
import { Table, Alert } from 'antd'; // import PropTypes from 'prop-types';

import classnames from 'classnames';
import { Bind } from 'lodash-decorators';
import intl from "../../utils/intl";
import styles from "./index.less?modules";
var List = (_dec = Bind(), _dec(_class = /*#__PURE__*/function (_PureComponent) {
  _inherits(List, _PureComponent);

  var _super = _createSuper(List);

  function List(props) {
    var _this;

    _classCallCheck(this, List);

    _this = _super.call(this, props);

    _this.handleOnChange = function (pagination, filters, sortInfo) {
      var onChange = _this.props.onChange;

      if (!onChange) {
        return;
      }

      onChange(_objectSpread(_objectSpread({}, pagination), {}, {
        orderField: sortInfo.columnKey,
        orderMethod: sortInfo.order
      }));
    };

    _this.handleRowSelectChange = function (selectedRowKeys, selectedRows) {
      if (_this.props.onSelectRow) {
        _this.props.onSelectRow(selectedRows);
      }

      if (_this.props.rowSelection && _this.props.rowSelection.onChange) {
        _this.props.rowSelection.onChange(selectedRowKeys, selectedRows);
      }

      if (!_this.props.rowSelection) {
        _this.setState({
          selectedRowKeys: selectedRowKeys
        });
      }
    };

    _this.state = {
      selectedRowKeys: []
    };
    return _this;
  } //  eslint-disable-next-line


  _createClass(List, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      // 将selectedRowKeys 置为空数组,防止 antd 遍历时出错
      if (!nextProps.selectedRows || nextProps.selectedRows.length === 0) {
        this.setState({
          selectedRowKeys: []
        });
      }
    }
    /**
     * 分页或者排序改变
     * @param {Object} pagination 分页信息
     * @param {Array} filters 过滤的字段
     * @param {Object} sortInfo 排序的信息
     */

  }, {
    key: "render",
    value: function render() {
      var _classnames;

      var _this$props = this.props,
          description = _this$props.description,
          scrollWidth = _this$props.scrollWidth,
          wrapStyle = _this$props.wrapStyle,
          singleCol = _this$props.singleCol,
          sortInfo = _this$props.sortInfo,
          tableProps = _objectWithoutProperties(_this$props, _excluded);

      var newTableProps = _objectSpread({}, tableProps);

      if (sortInfo) {
        // 判断是否有排序字段
        newTableProps.columns = tableProps.columns.map(function (item) {
          if (sortInfo[item.dataIndex]) {
            return _objectSpread(_objectSpread({}, item), {}, {
              sorter: true
            });
          }

          return item;
        });
      }

      var selectedRowKeys = this.state.selectedRowKeys;

      if (newTableProps.pagination) {
        // 分页信息
        newTableProps.pagination = _objectSpread({
          showQuickJumper: true,
          showSizeChanger: true,
          pageSizeOptions: ['10', '20', '50', '100', '300', '500'],
          showTotal: function showTotal(total, range) {
            return intl.get('cpaas.common.pagination.total', {
              range1: range[0],
              range2: range[1],
              total: total
            }).d("\u663E\u793A ".concat(range[0], " - ").concat(range[1], " \u5171 ").concat(total, " \u6761"));
          }
        }, tableProps.pagination);
      }

      if (newTableProps.bordered) {
        newTableProps.size = 'small';
      } // 设置列的宽度


      if (singleCol) {
        newTableProps.scroll = {
          x: false
        };
      } else if (newTableProps.scroll === false) {
        delete newTableProps.scroll;
      } else {
        var newScrollWidth = scrollWidth || tableProps.columns.reduce(function (result, column) {
          return (// 列表字段 默认宽度 120
            result + column.width || 120
          );
        }, 0);
        newTableProps.scroll = _objectSpread(_objectSpread({}, newTableProps.scroll), {}, {
          x: newScrollWidth
        });
      } // 设置选中记录状态,当记录中有 disabled 属性时 会禁止选中


      var rowSelection = tableProps.rowSelection === null ? undefined : tableProps.rowSelection || {
        selectedRowKeys: selectedRowKeys,
        onChange: this.handleRowSelectChange,
        getCheckboxProps: function getCheckboxProps(record) {
          return {
            disabled: record.disabled
          };
        }
      };
      return /*#__PURE__*/React.createElement("div", {
        style: _objectSpread({
          width: '100%'
        }, wrapStyle)
      }, rowSelection !== undefined && /*#__PURE__*/React.createElement(Alert, {
        message: /*#__PURE__*/React.createElement("div", {
          className: styles.info
        }, intl.get('cpaas.common.components.list.select').d('已选择'), ' ', /*#__PURE__*/React.createElement("a", {
          style: {
            fontWeight: 600
          }
        }, rowSelection.selectedRowKeys.length, " "), intl.get('cpaas.common.components.list.item').d('项'), "\xA0\xA0", rowSelection.selectedRowKeys.length > 0 && /*#__PURE__*/React.createElement("a", {
          onClick: this.cleanSelectedKeys,
          style: {
            marginLeft: 24
          }
        }, intl.get('cpaas.common.button.clear').d('清空')), description && /*#__PURE__*/React.createElement("span", {
          className: styles.descript
        }, "\xA0\xA0\xA0\xA0", description)),
        type: "info",
        showIcon: true,
        style: {
          marginBottom: 8
        }
      }), /*#__PURE__*/React.createElement(Table, Object.assign({
        rowSelection: rowSelection
      }, newTableProps, {
        onChange: this.handleOnChange,
        className: classnames((_classnames = {}, _defineProperty(_classnames, styles.table, true), _defineProperty(_classnames, styles.singleCol, singleCol), _classnames))
      })));
    }
  }]);

  return List;
}(PureComponent)) || _class);
export { List as default };